package CompleteFutureDemo;

import common.CalcTask;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;

/**
 * @author chao
 * @create 2022-02-06-16:32
 */
public class ExceptTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ForkJoinPool joinPool = new ForkJoinPool();
        CompletableFuture<Double> cf = CompletableFuture.supplyAsync(() -> {
            if(false){
                throw new RuntimeException("test");
            }
            return new CalcTask().RunWithResult();
        }, joinPool);
        CompletableFuture<Double> cf2 = cf.exceptionally((param) -> {
            System.out.println("发生异常：" + System.currentTimeMillis());
            System.out.println(param);
            return -1.1;
        });
        CompletableFuture<Void> cf3 = cf.thenAccept((result) -> {
            System.out.println("thenAccept -> 结果是：" + result);
        });
        System.out.println("主线程执行："+System.currentTimeMillis());
        System.out.println("异常发生时的结果："+cf2.get());
        System.out.println("主线程结束："+System.currentTimeMillis());

    }
}
